package thread;

public class ComRunThread {
    private Monitor monitor;
    private String sourceName;
    private ComRun task;

    public ComRunThread(String str,String sourceName){
        this.sourceName=sourceName;
        switch (str){
            case "cpp":
                task=new ComRunCpp(sourceName);
                break;
            case "python":
                task=new ComRunPy(sourceName);
                break;
            case "java":
                task=new ComRunJava(sourceName);
                break;
            default:
                System.out.println("无此操作哦");
                break;
        }
    }
    public String StartUp(){
        Thread t = new Thread(task);
        monitor=new Monitor(t,2);
        Thread m = new Thread(monitor);
        m.setDaemon(true);
        t.start();
        m.start();
        try {
            t.join();
            return task.getResult();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "错误";
    }

    static class Monitor implements Runnable{
        private Thread task;//监视的任务
        private int time;//监视的时间

            public Monitor(Thread task,int time){
            this.task=task;
            this.time=time;
        }
        @Override
        public void run() {
            try {
                Thread.sleep(time*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            task.interrupt();//time秒后中断线程
        }
    }
    static class ComRun implements Runnable{
        @Override
        public void run() {
            System.out.println("run");
        }

        public String getResult() {
            return null;
        }
    }

}
